﻿Imports System
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Runtime.DebugEngine
    Friend NotInheritable Class InstanceTable
        Inherits BreakSafeBase([Of] InstanceMap)
        Implements IInstanceTable
        ' Methods
        Public Sub New(ByVal controllerManagedThreadId As Integer)
            MyBase.New(controllerManagedThreadId)
        End Sub

        Public Sub AddInstance(ByVal instanceId As Guid, ByVal rootActivity As Activity)
            Try 
                MyBase.Lock
                Dim writerData As InstanceMap = MyBase.GetWriterData
                writerData.Item(instanceId) = New InstanceData(rootActivity)
                MyBase.SaveData(writerData)
            Finally
                MyBase.Unlock
            End Try
        End Sub

        Public Function GetRootActivity(ByVal instanceId As Guid) As Activity
            Dim rootActivity As Activity
            Try 
                MyBase.Lock
                rootActivity = MyBase.GetReaderData.Item(instanceId).RootActivity
            Finally
                MyBase.Unlock
            End Try
            Return rootActivity
        End Function

        Public Sub RemoveInstance(ByVal instanceId As Guid)
            Try 
                MyBase.Lock
                Dim writerData As InstanceMap = MyBase.GetWriterData
                writerData.Remove(instanceId)
                MyBase.SaveData(writerData)
            Finally
                MyBase.Unlock
            End Try
        End Sub

        Private Function System.Workflow.Runtime.DebugEngine.IInstanceTable.GetActivity(ByVal instanceId As String, ByVal activityQualifiedName As String) As Activity Implements IInstanceTable.GetActivity
            Dim activity2 As Activity
            Try 
                MyBase.Lock
                activity2 = DebuggerHelpers.ParseActivity(MyBase.GetReaderData.Item(New Guid(instanceId)).RootActivity, activityQualifiedName)
            Finally
                MyBase.Unlock
            End Try
            Return activity2
        End Function

        Public Sub UpdateRootActivity(ByVal instanceId As Guid, ByVal rootActivity As Activity)
            Try 
                MyBase.Lock
                Dim writerData As InstanceMap = MyBase.GetWriterData
                writerData.Item(instanceId).RootActivity = rootActivity
                MyBase.SaveData(writerData)
            Finally
                MyBase.Unlock
            End Try
        End Sub

    End Class
End Namespace

